package com.juns.wechat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.text.TextUtils; import android.util.Log; import com.baidu.frontia.api.FrontiaPushMessageReceiver; import com.juns.wechat.common.BaiduUtils; import com.juns.wechat.common.Utils; /** * Push消息处理receiver */ public class PushMessageReceiver extends FrontiaPushMessageReceiver { /** TAG to Log */ public static final String TAG = PushMessageReceiver.class.getSimpleName(); /** * 调用PushManager.startWork后,sdk将对push * server发起绑定请求,这个过程是异步的。绑定请求的结果通过onBind返回。 如果您需要用单播推送,需要把这里获取的channel * id和user id上传到应用server中,再调用server接口用channel id和user id给单个手机或者用户推送。 * * @param context * BroadcastReceiver的执行Context * @param errorCode * 绑定接口返回值,0 - 成功 * @param appid * 应用id。errorCode非0时为null * @param userId * 应用user id。errorCode非0时为null * @param channelId * 应用channel id。errorCode非0时为null * @param requestId * 向服务端发起的请求id。在追查问题时有用; * @return none */ @Override public void onBind(Context context, int errorCode, String appid, String userId, String channelId, String requestId) { String responseString = "onBind errorCode=" + errorCode + " appid=" + appid + " userId=" + userId + " channelId=" + channelId + " requestId=" + requestId; Log.d(TAG, responseString); // 绑定成功,设置已绑定flag,可以有效的减少不必要的绑定请求 if (errorCode == 0) { BaiduUtils.setBind(context, true); } // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑 // updateContent(context, responseString); } /** * 接收透传消息的函数。 * * @param context * 上下文 * @param message * 推送的消息 * @param customContentString * 自定义内容,为空或者json字符串 */ @Override public void onMessage(Context context, String message, String customContentString) { String messageString = "透传消息 message=\"" + message + "\" customContentString=" + customContentString; Log.d(TAG, messageString); // 自定义内容获取方式,mykey和myvalue对应透传消息推送时自定义内容中设置的键和值 if (!TextUtils.isEmpty(customContentString)) { JSONObject customJson = null; try { customJson = new JSONObject(customContentString); String myvalue = null; if (!customJson.isNull("mykey")) { myvalue = customJson.getString("mykey"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑 updateContent(context, messageString); } /** * 接收通知点击的函数。注:推送通知被用户点击前,应用无法通过接口获取通知的内容。 * * @param context * 上下文 * @param title * 推送的通知的标题 * @param description * 推送的通知的描述 * @param customContentString * 自定义内容,为空或者json字符串 */ @Override public void onNotificationClicked(Context context, String title, String description, String customContentString) { String notifyString = "通知点击 title=\"" + title + "\" description=\"" + description + "\" customContent=" + customContentString; Log.d(TAG, notifyString); // 自定义内容获取方式,mykey和myvalue对应通知推送时自定义内容中设置的键和值 if (!TextUtils.isEmpty(customContentString)) { JSONObject customJson = null; try { customJson = new JSONObject(customContentString); String myvalue = null; if (!customJson.isNull("mykey")) { myvalue = customJson.getString("mykey"); Log.d(TAG, myvalue); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String str_notify = "[" + title + "]:" + description; // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑 updateContent(context, str_notify); } /** * setTags() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示某些tag已经设置成功;非0表示所有tag的设置均失败。 * @param successTags * 设置成功的tag * @param failTags * 设置失败的tag * @param requestId * 分配给对云推送的请求的id */ @Override public void onSetTags(Context context, int errorCode, List<String> sucessTags, List<String> failTags, String requestId) { String responseString = "onSetTags errorCode=" + errorCode + " sucessTags=" + sucessTags + " failTags=" + failTags + " requestId=" + requestId; Log.d(TAG, responseString); // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑 // updateContent(context, responseString); } /** * delTags() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示某些tag已经删除成功;非0表示所有tag均删除失败。 * @param successTags * 成功删除的tag * @param failTags * 删除失败的tag * @param requestId * 分配给对云推送的请求的id */ @Override public void onDelTags(Context context, int errorCode, List<String> sucessTags, List<String> failTags, String requestId) { String responseString = "onDelTags errorCode=" + errorCode + " sucessTags=" + sucessTags + " failTags=" + failTags + " requestId=" + requestId; Log.d(TAG, responseString); // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑 // updateContent(context, responseString); } /** * listTags() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示列举tag成功;非0表示失败。 * @param tags * 当前应用设置的所有tag。 * @param requestId * 分配给对云推送的请求的id */ @Override public void onListTags(Context context, int errorCode, List<String> tags, String requestId) { String responseString = "onListTags errorCode=" + errorCode + " tags=" + tags; Log.d(TAG, responseString); // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑 // updateContent(context, responseString); } /** * PushManager.stopWork() 的回调函数。 * * @param context * 上下文 * @param errorCode * 错误码。0表示从云推送解绑定成功;非0表示失败。 * @param requestId * 分配给对云推送的请求的id */ @Override public void onUnbind(Context context, int errorCode, String requestId) { String responseString = "onUnbind errorCode=" + errorCode + " requestId = " + requestId; Log.d(TAG, responseString); // 解绑定成功,设置未绑定flag, if (errorCode == 0) { BaiduUtils.setBind(context, false); } // Demo更新界面展示代码,应用请在这里加入自己的处理逻辑 // updateContent(context, responseString); } @SuppressLint("SimpleDateFormat") private void updateContent(Context context, String content) { Log.d(TAG, "updateContent"); String logText = "" + BaiduUtils.logStringCache; if (!logText.equals("")) { logText += "\n"; } Long tsLong = System.currentTimeMillis() / 1000; String ts = tsLong.toString(); Utils.putValue(context, "Time", ts); Utils.putValue(context, "Content", content); GloableParams.isHasPulicMsg = true; SimpleDateFormat sDateFormat = new SimpleDateFormat("HH-mm-ss"); logText += sDateFormat.format(new Date()) + ": "; logText += content; BaiduUtils.logStringCache = logText; Intent intent = new Intent(); intent.setClass(context.getApplicationContext(), SplashActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.getApplicationContext().startActivity(intent); } }